package windowUnits;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.text.Document;

public class TextFieldWithDatePicker extends JPanel {
	
	private final JTextField dateField = new JTextField(); 
	private final JButton dateButton = new JButton();
	
	public TextFieldWithDatePicker() {
		super();
		init();
	}

	public TextFieldWithDatePicker(LayoutManager arg0) {
		super(arg0);
		init();
	}

	public TextFieldWithDatePicker(boolean arg0) {
		super(arg0);
		init();
	}

	public TextFieldWithDatePicker(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		init();
	}
	
	private void init() {
		setLayout(new BorderLayout());
		setBorder(new LineBorder(new Color(213,213,213), 1, false));
		dateField.setPreferredSize(new Dimension(200,20));
		dateButton.setPreferredSize(new Dimension(20,20));
		dateButton.addActionListener(new DateButtonActionListener());
		dateButton.setText(">>");
		add(dateField, BorderLayout.CENTER);
		add(dateButton, BorderLayout.EAST);
	}
	private class DateButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			dateButton_actionPerformed(arg0);
		}
	}
	protected void dateButton_actionPerformed(ActionEvent arg0) {		
		MyDatePicker frame = new MyDatePicker(dateField);
		Point p = new Point();
		p = this.getLocationOnScreen();
		p.translate(dateField.getWidth(), this.getHeight());
		frame.setLocation(p);
		frame.setVisible(true);
	}
	public Document getDocument(){
		return dateField.getDocument();
	}
	public void setText(String text){
		dateField.setText(text);
	}
	public String getText(){
		return dateField.getText();
	}
}
